package com.hqyj.hrms.modules.personnel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.personnel.dao.EmployeeMapper;
import com.hqyj.hrms.modules.personnel.dao.RecordMapper;
import com.hqyj.hrms.modules.personnel.entity.Employee;
import com.hqyj.hrms.modules.personnel.entity.Record;
import com.hqyj.hrms.modules.personnel.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Autor zyj
 * @Date 2021/9/29
 **/
@Service
public class RecordServiceImpl implements RecordService {
    //注入mapper
    @Autowired(required = false)
    RecordMapper recordMapper;

    @Override
    public HashMap<String, Object> list(Record record) {
        HashMap<String, Object> map = new HashMap<String, Object>();
//        //创建查询插件对象
//        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//
//        //条件   列名   参数
//        queryWrapper.eq(
//                record.getRecordName() != null && !record.getRecordName().equals(""),
//                "record_name",
//                record.getRecordName());

        //第一步创建分页对象
        Page<Record> recordPage = new Page<>(record.getPage(), record.getRow());

        String keyWord =record.getKeyWord();
        QueryWrapper wrapper = new QueryWrapper<Record>()
                .like(StringUtils.isNotBlank(keyWord),"emp_id",keyWord).or()
                .like(StringUtils.isNotBlank(keyWord),"record_name",keyWord).or()
                .like(StringUtils.isNotBlank(keyWord),"record_synopsis",keyWord).or()
                .like(StringUtils.isNotBlank(keyWord),"record_remarks",keyWord);

        //第二页  查询分页  获取分页数据集
        Page<Record> pageList = recordMapper.selectPage(recordPage, wrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
//        List<UserInfo> list = userInfoMapper.selectList(null);
//        map.put("info",list);
        return map;
    }

    @Override
    public HashMap<String, Object> add(Record record) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = recordMapper.insert(record);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Record record) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = recordMapper.updateById(record);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = recordMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }
}
